import Vue from 'vue';
import api from "@/service/api";
import http from "@/service/http";

//状态
const state={
	  listLoading: false,
      pageSize: 20,
      totalNum: 0,
	  currentPage: 1,
	  tableData:[],
	  selects:[],//多个 
	  isDialogShow:false,
	  ableTag:'',
	//过虑条件
	filters:{ 
		status:''
	}
}

//getter
const getters={
	tableData:state=>state.tableData,
	selects:state=>state.selects,
	pageSize:state=>state.pageSize,
	currentPage:state=>state.currentPage,
	totalNum:state=>state.totalNum,
	listLoading:state=>state.listLoading,
	filters:state=>state.filters,
	isDialogShow:state=>state.isDialogShow,
	ableTag:state=>state.ableTag
}

//actions
const actions={
	searchAssets({commit,state}) { 
		commit('listLoading',true);
		var search = { pageNum: state.currentPage,
			 pageSize: state.pageSize,status:state.ableTag };
		http.post(api.sysApi.searchAssets, search).then(data => {
			if(!data.success){
				return;
			}
			commit('loadList',data);
		  commit('listLoading',false);
		});
	  },
	//表格选择行
	handleSelectionChange({commit,dispatch},selects){
		commit('selectsChange',selects);
	},
	//表格翻页
	handleCurrentChange({commit,dispatch},currentPage){
		commit('currentChange',currentPage);
		dispatch('searchAssets');
	},
	//表格设置每页条数
	handleSizeChange({commit,dispatch},pageSize){
		commit('sizeChange',pageSize);
		dispatch('searchAssets');
	},
	//重置过虑器
	resetFilters({commit,dispatch},filters){
		commit('resetFilters',filters);
		dispatch('searchAssets');
	},
	//表格载入状态
	listLoading({commit},isShow){
		commit('listLoading',isShow);
	},
	//是否来自选择
	toSelect({commit},isToSelect){
		commit('isSelectAsset',isToSelect);
	},
	//是否来自选择
	toUsingSelect({commit},isToSelect){
		commit('usingAsset',isToSelect);
	},
	//是否显示为dialog
	showDialog({commit},isDialogShow){
		commit('setAssetDialogShow',isDialogShow);
	}
}

//mutations
const mutations={
	//角色列表数据状态变更
	loadList(state,param){
		state.totalNum=param.totalNum;
		state.tableData=param.data;
	},
	//表格载入状态变更
	listLoading(state,isShow){
		state.listLoading=isShow;
	},
	//表格选中行数据状态变更
	selectsChange(state,selects){ 
		if(selects.length==0){
			state.selects=[];
		}else{
			state.selects=selects;
		}
	},
	//表格翻页状态变更
	currentChange(state,currentPage){
		state.currentPage=currentPage;
	},
	//表格每页显示条数状态变更
	sizeChange(state,pageSize){
		state.pageSize=pageSize;
	},
	//查询条件过虑器状态充更
	resetFilters(state,filters){
		state.filters=filters;
	},
	//是否来自选择用户
	isSelectAsset(state,isToSelect){
		state.isToSelect=isToSelect;
		state.ableTag = isToSelect?1:'';
	}, 
	//选择正在使用的资产
	usingAsset(state,isToSelect){
		state.isToSelect=isToSelect;
		state.ableTag =isToSelect?2:'';//正在使用
	}, 
	setAssetDialogShow(state,isDialogShow){
		state.isDialogShow=isDialogShow;
	}
}

export default{
	namespaced:true,
	state,
	getters,
	actions,
	mutations
}